home *** CD-ROM | disk | FTP | other *** search
- /*
- *
- * file.c
- *
- * File Input/Output routines.
- *
- *
- * Author: Rob Johnston
- * Date: Thursday, January 26, 1992
- *
- * Copyright © 1992 Apple Computer, Inc.
- *
- */
-
- #include "globals.h"
- #include "prototypes.h"
- #include "resources.h"
-
-
- short ReadDocumentFile(Document *theDocument)
-
- { long count;
- short theResult;
- char buffer[256];
- TextStyle theStyle;
-
- SetCursor(*GetCursor(watchCursor));
-
- TESetSelect(0, (**(theDocument->theTE)).teLength, theDocument->theTE);
- TEDelete(theDocument->theTE);
-
- if (theResult = SetFPos(theDocument->fRefNum, fsFromStart, 0))
- return(theResult);
-
- do {
- count = 256;
- theResult = FSRead(theDocument->fRefNum, &count, &buffer);
- TEInsert(&buffer, count, theDocument->theTE);
- } while (theResult == noErr);
-
- TESetSelect(0, 32767, theDocument->theTE);
- theStyle.tsFont = 21;
- theStyle.tsSize = 12;
- TESetStyle(doFont + doSize, &theStyle, true, theDocument->theTE);
- TESetSelect(0, 0, theDocument->theTE);
- theDocument->dirty = false;
-
- return(noErr);
- }
-
-
- short WriteDocumentFile(Document *theDocument)
-
- { short theResult;
- long length;
- char *bufPtr;
-
- SetCursor(*GetCursor(watchCursor));
-
- if (! theDocument->fRefNum)
- return(fnOpnErr);
-
- if (theResult = SetFPos(theDocument->fRefNum, fsFromStart, 0))
- return(theResult);
-
- length = (**(theDocument->theTE)).teLength;
- bufPtr = *((**(theDocument->theTE)).hText);
-
- if (theResult = FSWrite(theDocument->fRefNum, &length, bufPtr))
- return(theResult);
-
- if (theResult = SetEOF(theDocument->fRefNum, length))
- return(theResult);
- }
-
-
- void DoNewDocument()
-
- { Document *theDocument;
-
- if (theDocument = NewDocument()) {
- ShowWindow(theDocument->theWindow);
- }
- }
-
-
- OSErr DoOpenFile(FSSpec *theFile)
-
- { OSErr result;
- short refNum;
- Document *theDocument;
-
- if (result = FSpOpenDF(theFile, fsRdWrPerm, &refNum))
- return(result);
-
- if (theDocument = NewDocument()) {
- theDocument->fRefNum = refNum;
- if (ReadDocumentFile(theDocument)) {
- SysBeep(1);
- }
- SetWTitle(theDocument->theWindow, theFile->name);
- AdjustScrollBar(theDocument);
- ShowWindow(theDocument->theWindow);
- } else {
- SysBeep(1);
- FSClose(refNum);
- return(memFullErr);
- }
- return(noErr);
- }
-
-
-
- void DoOpenDocument()
-
- { short refNum;
- SFTypeList theTypeList;
- StandardFileReply theReply;
- Document *theDocument;
-
- theTypeList[0] = 'TEXT';
- StandardGetFile(0L, 1, theTypeList, &theReply);
-
- if (theReply.sfGood) {
- DoOpenFile(&theReply.sfFile);
-
- /* if (FSpOpenDF(&theReply.sfFile, fsRdWrPerm, &refNum)) {
- SysBeep(1);
- return;
- }
- if (theDocument = NewDocument()) {
- theDocument->fRefNum = refNum;
- if (ReadDocumentFile(theDocument)) {
- SysBeep(1);
- }
- SetWTitle(theDocument->theWindow, theReply.sfFile.name);
- AdjustScrollBar(theDocument);
- ShowWindow(theDocument->theWindow);
- } else {
- SysBeep(1);
- FSClose(refNum);
- }
- */
-
- }
- }
-
-
- short DoSaveAsDocument(Document *theDocument)
-
- { short theResult;
- Str255 thePrompt, theName;
- StandardFileReply theReply;
-
- if (! theDocument)
- return(false);
-
- GetIndString(&thePrompt, FileStringsID, slSavePromptIndex);
- GetWTitle(theDocument->theWindow, &theName);
- StandardPutFile(&thePrompt, &theName, &theReply);
-
- if (theReply.sfGood) {
- if (!theReply.sfReplacing) {
- theResult = FSpCreate(&theReply.sfFile, FileCreator, FileType, theReply.sfScript);
- if (theResult) {
- SysBeep(1);
- return(false);
- }
- }
- if (theDocument->fRefNum) {
- theResult = FSClose(theDocument->fRefNum);
- }
- theResult = FSpOpenDF(&theReply.sfFile, fsRdWrPerm, &theDocument->fRefNum);
- if (theResult) {
- SysBeep(1);
- return(false);
- }
-
- if (theResult = WriteDocumentFile(theDocument)) {
- SysBeep(1);
- return(false);
- }
-
- SetWTitle(theDocument->theWindow, theReply.sfFile.name);
- theDocument->dirty = false;
-
- } else {
- return(false);
- }
- return(true);
- }
-
-
- short DoSaveDocument(Document *theDocument)
-
- {
- if (! theDocument)
- return(false);
-
- if (theDocument->fRefNum) {
- if (WriteDocumentFile(theDocument)) {
- SysBeep(1);
- return(false);
- } else {
- theDocument->dirty = false;
- }
- return(true);
- } else {
- return(DoSaveAsDocument(theDocument));
- }
- }
-
-
- void DoRevertDocument(Document *theDocument)
-
- { Str255 theName;
-
- if (! theDocument)
- return;
-
- if (theDocument->fRefNum) {
- GetWTitle(theDocument->theWindow, &theName);
- ParamText(&theName, "", "", "");
- if (Alert(idRevertALRT, 0L) == 1) {
- if (ReadDocumentFile(theDocument))
- SysBeep(1);
- }
- }
- }
-
-
-